<?php
/**
 * Created by PhpStorm.
 * User: wolferhua
 * Email: 358273385@qq.com
 * Date: 2015/9/19 0019
 * Time: 下午 17:30
 */

namespace Common\Behavior;


class HcBuildBehavior
{
    // 行为扩展的执行入口必须是run
    public function run(&$params)
    {
        defined('BUCKET_PATH') or define('BUCKET_PATH', dirname(APP_PATH) . '/Data/');      //空间文件目录
        defined('BUCKET_LINK_PATH') or define('BUCKET_LINK_PATH', BUCKET_PATH . 'Link/'); //空间文件目录
        defined('BUCKET_FILE_PATH') or define('BUCKET_FILE_PATH', BUCKET_PATH . 'File/'); //空间文件目录
        $dirs = array(
            BUCKET_PATH,
            BUCKET_LINK_PATH,
            BUCKET_FILE_PATH
        );
        foreach ($dirs as $dir) {
            if (!is_dir($dir)) {
                mkdir($dir, 0755, true);
            }
        }
        self::buildDirSecure($dirs);
    }


    // 生成目录安全文件
    static public function buildDirSecure($dirs = array())
    {
        // 目录安全写入（默认开启）
        defined('BUILD_DIR_SECURE') or define('BUILD_DIR_SECURE', true);
        if (BUILD_DIR_SECURE) {
            defined('DIR_SECURE_FILENAME') or define('DIR_SECURE_FILENAME', 'index.html');
            defined('DIR_SECURE_CONTENT') or define('DIR_SECURE_CONTENT', ' ');
            // 自动写入目录安全文件
            $content = DIR_SECURE_CONTENT;
            $files = explode(',', DIR_SECURE_FILENAME);
            foreach ($files as $filename) {
                foreach ($dirs as $dir) {
                    if (!is_file($dir . $filename))
                        file_put_contents($dir . $filename, $content);
                }
            }
        }
    }
}